package com.yearner.reviewplan.manage.system;

import java.util.List;
import java.util.Map;

import com.yearner.reviewplan.common.aspect.AuthTokenV1;
import com.yearner.reviewplan.common.model.Result;
import com.yearner.reviewplan.manage.system.bean.RouterVo;
import com.yearner.reviewplan.manage.system.entity.UserRouter;
import com.yearner.reviewplan.manage.system.service.UserRouterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;


/**
 * 路由表
 *
 * @author Pengyg
 * @since 2020-01-12
 */
@Api(value = "UserRouter")
@RestController
@RequestMapping("api/system/router")
public class UserRouterController {
    private final UserRouterService service;

    private List<RouterVo> routerVoList;
    private boolean updateVos = false;

    public UserRouterController(UserRouterService service) {
        this.service = service;
    }

    /**
     * 列表
     */
    @AuthTokenV1
    @ApiOperation(value = "list")
    @GetMapping("/list")
    public Result<Object> list(@RequestParam Map<String, Object> params){

        List<RouterVo> allRouter = service.getAllRouter();
        return Result.success(allRouter,allRouter.size());
    }


    /**
     * 信息详情
     */
    @AuthTokenV1
    @GetMapping("/info/{id}")
    public Result<Object> info(@PathVariable("id") String id){
		UserRouter router = service.getById(id);
        return Result.success(router);
    }

    /**
     * 保存
     */
    @AuthTokenV1
    @PostMapping("/save")
    public Result<String> save(@RequestBody UserRouter entity){
		service.save(entity);
        updateVos = true;
        return Result.success();
    }

    /**
     * 修改
     */
    @AuthTokenV1
    @PostMapping("/update")
    public Result<String> update(@RequestBody UserRouter entity){
		service.updateById(entity);
        updateVos = true;
        return Result.success();
    }

    /**
     * 删除
     */
    @AuthTokenV1
    @DeleteMapping("/delete")
    public Result<String> delete(@RequestBody List<Integer> ids){
		service.deleteById(ids);
        updateVos = true;
        return Result.success();
    }

    private void updateHandleList() {
        if (updateVos || routerVoList ==null) {
            this.routerVoList = service.getRouterVos();
        }
    }

    List<RouterVo> getRouterVoList() {
        updateHandleList();
        return routerVoList;
    }
}
